//
//  WebViewVCV.m
//  Koina
//
//  Created by welldone on 13-12-6.
//  Copyright (c) 2013年 net.koina. All rights reserved.
//

#import "WebViewVCV.h"
#import "ProgressLableView.h"
#import "JSONKit.h"
#import "StringTool.h"
#import "BdTV.h"
#import "config.h"
#import "NJKWebViewProgress.h"




@interface WebViewVCV ()<UIWebViewDelegate, NJKWebViewProgressDelegate>
-(void)btnOnclick:(UIButton*)sender;
@property (nonatomic,retain)UIWebView *myWebView;
@property (nonatomic,retain)ProgressLableView *progressLab;
@property (strong, nonatomic) NJKWebViewProgress *progressProxy;
@end

@implementation WebViewVCV
@synthesize myWebView,progressLab;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
   
    
    [self.titleView setMianTitle:@"加载中..."];
    [[self.titleView doneBtn]setHidden:YES];
    [[self.titleView doneBtn] removeFromSuperview];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.titleView];
    
    [(UIScrollView *)[[myWebView subviews] objectAtIndex:0] setBounces:NO];
    myWebView.scalesPageToFit = true;
    
    _progressProxy = [[NJKWebViewProgress alloc] init];
    myWebView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    
    CGFloat progressBarHeight = 2.f;
    CGRect navigaitonBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, navigaitonBarBounds.size.height - progressBarHeight, navigaitonBarBounds.size.width, progressBarHeight);
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}





-(void)loadDataByUrl:(NSString*)url{

    CGRect pmRect = [ UIScreen mainScreen ].applicationFrame;
    myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, PMW, pmRect.size.height-44)];
    [myWebView setBackgroundColor:[UIColor clearColor]];
    myWebView.delegate = self;
    [self.view addSubview:myWebView];
    
    NSURL *nsUrl = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:nsUrl];
    [myWebView loadRequest:request];
    
    
    progressLab = [[ProgressLableView alloc]initWithFrame:CGRectMake(0, 200, PMW, 100)];
//    progressLab = [[NJKWebViewProgress alloc]initWithFrame:CGRectMake(0, 200, PMW, 100)];

    [self.view addSubview:progressLab];
    
}


-(void)setMainTitle:(NSString*)title{
    if (self.titleView) {
        [self.titleView setMianTitle:title];
    }
}


-(void)btnOnclick:(UIButton*)sender{
    if (sender.tag==78) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag==79){//前进
        [myWebView goForward];
    }else if (sender.tag==80){//后退
        [myWebView goBack];
    }
}




-(void)progressChange:(float)pro{
    [progressLab setProgess:pro animation:YES];
}


#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    
    if (progress == 0.0) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        [self progressChange:0];
        [UIView animateWithDuration:0.27 animations:^{
            [self.progressLab setHidden:false];
        }];
    }
    if (progress == 1.0) {
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [UIView animateWithDuration:0.27 delay:progress - progressLab.getCurrProgress options:0 animations:^{
             [self.progressLab setHidden:true];
        } completion:nil];
    }
    
    [self progressChange:progress];
    NSLog(@"updateProgress");
}




@end
